﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Collections;
using System.Text;
using System.Text.RegularExpressions;


public class HelpMethod
{
    public static string MD5(string input)
    {
        System.Security.Cryptography.MD5CryptoServiceProvider md5Hasher = new System.Security.Cryptography.MD5CryptoServiceProvider();
        byte[] hashedBytes;
        System.Text.UTF8Encoding encoder = new System.Text.UTF8Encoding();
        hashedBytes = md5Hasher.ComputeHash(encoder.GetBytes(input));

        StringBuilder s = new StringBuilder();
        foreach (byte p in hashedBytes)
        {
            s.Append(p.ToString("x").ToLower());
        }
        return s.ToString();
    }

    public static bool IsEmail(string input)
    {
        Regex RegexObj = new Regex(@"\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*");
        return RegexObj.Match(input).Success;
    }

    public static bool IsUserID(string input)
    {
        Regex RegexObj = new Regex(@"[a-zA-Z_]+[a-zA-Z0-9_]");
        return RegexObj.Match(input).Success;
    }

    public static bool IsPassword(string input)
    {
        Regex RegexObj = new Regex(@"[a-zA-Z0-9_@$]");
        return RegexObj.Match(input).Success;
    }

    public static string ChangeDatetimeToString(DateTime dt)
    {
        string result;
        long tickDiff = DateTime.Now.Ticks - dt.Ticks;
        tickDiff = tickDiff / 10000000; //have seconds now
        int day = (int)(tickDiff / 86400);
        if (day == 0)
            result = dt.Hour + "h" + dt.Minute + "'|Hôm nay";
        else if (day == 1)
            result = dt.Hour + "h" + dt.Minute + "'|Hôm qua";
        else if (day == 2)
            result = dt.Hour + "h" + dt.Minute + "'|Hôm kia";
        else
            result = "cách đây " + dt.ToString() + " ngày";
        return result;
    }

    public static string CatString(string input, int max)
    {
        if (input.Length > max)
            return input.Substring(0, max) + "...";
        else
            return input;
    }

    public static string RemoveHtmlTag(string input)
    {
        input = input.Replace("&lt;", "<");
        input = input.Replace("&gt;", ">");
        input = input.Replace("&nbsp;", " ");

        char[] array = new char[input.Length];
        int arrayIndex = 0;
        bool inside = false;

        for (int i = 0; i < input.Length; i++)
        {
            char let = input[i];
            if (let == '<')
            {
                inside = true;
                continue;
            }
            if (let == '>')
            {
                inside = false;
                continue;
            }
            if (!inside)
            {
                array[arrayIndex] = let;
                arrayIndex++;
            }
        }
        return new string(array, 0, arrayIndex);
    }


    public static string FormatStringForUrl( String input)
    {
        String strNotSign = RemoveSign4VietnameseString(input);
        strNotSign = strNotSign.Replace(" ", "-");
        strNotSign = strNotSign.Replace("@", "");
        strNotSign = strNotSign.Replace("%", "");
        strNotSign = strNotSign.Replace("#", "");
        strNotSign = strNotSign.Replace("$", "");
        strNotSign = strNotSign.Replace("?", "");
        strNotSign = strNotSign.Replace("&", "");
        strNotSign = strNotSign.Replace(".", "");
        return strNotSign;
    }

    public static string RemoveSign4VietnameseString(string str)
    {
        string[] VietnameseSigns = new string[]
            {
                "aAeEoOuUiIdDyY",
                "áàạảãâấầậẩẫăắằặẳẵ",
                "ÁÀẠẢÃÂẤẦẬẨẪĂẮẰẶẲẴ",
                "éèẹẻẽêếềệểễ",
                "ÉÈẸẺẼÊẾỀỆỂỄ",
                "óòọỏõôốồộổỗơớờợởỡ",
                "ÓÒỌỎÕÔỐỒỘỔỖƠỚỜỢỞỠ",
                "úùụủũưứừựửữ",
                "ÚÙỤỦŨƯỨỪỰỬỮ",
                "íìịỉĩ",
                "ÍÌỊỈĨ",
                "đ",
                "Đ",
                "ýỳỵỷỹ",
                "ÝỲỴỶỸ"
            };

        //Tiến hành thay thế , lọc bỏ dấu cho chuỗi

        for (int i = 1; i < VietnameseSigns.Length; i++)
        {

            for (int j = 0; j < VietnameseSigns[i].Length; j++)

                str = str.Replace(VietnameseSigns[i][j], VietnameseSigns[0][i - 1]);

        }

        return str;
    }

    public static String CreateTwitter(String url, String title)
    {
        StringBuilder sb = new StringBuilder();
        sb.Append("<a ");
        sb.Append("hreft='https://twitter.com/share' class='twitter-share-button' data-via='MY-TWITTER-HANDLE' data-count='horizontal' ");
        sb.Append("data-text = '" + title + "' ");
        sb.Append("data-url = '" + url + "' >");
        sb.Append("Tweet");
        sb.Append("</a>");

        return sb.ToString();
    }

    public static String CreateFaceBook(String url, String title)
    {
        StringBuilder sb = new StringBuilder();
        sb.Append("<div ");
        sb.Append("class='fb-like' data-send='false' data-layout='button_count' data-show-faces='false' data-font='arial' ");
        sb.Append("data-href = '" + url + "' >");
        sb.Append("</div>");

        return sb.ToString();
    }

    public static String CreateGooglePlusOne(String url, String title)
    {
        StringBuilder sb = new StringBuilder();
        sb.Append("<div ");
        sb.Append("class='g-plusone' data-size='medium' ");
        sb.Append("data-href = '" + url + "' >");
        sb.Append("</div>");

        return sb.ToString();
    }

    /// <summary>
    /// $(function () {
    //Google +1
    //$.getScript("http://apis.google.com/js/plusone.js", null, true);

    //Twitter
    //$.getScript("http://platform.twitter.com/widgets.js", null, true);

    //Facebook
    // $.getScript("http://connect.facebook.net/en_US/all.js#xfbml=1",  function(){

    //   $('body').append('<div id="fb-root"></div>'); 

    //   FB.init({ status: true, cookie: true, xfbml: true }); 

    //}, true);
    //});
    /// </summary>
    /// <param name="url"></param>
    /// <param name="title"></param>
    /// <returns></returns>
    public static String CreateAllButtonShared(String url, String title)
    {
        StringBuilder sb = new StringBuilder();
        sb.Append("<ul class='social'>");

        // google +
        sb.Append("<li class='social-google' style='display:inline;'>");
        sb.Append(CreateGooglePlusOne(url, title));
        sb.Append("</li>");

        // Twitter +
        sb.Append("<li class='social-twitter' style='display:inline;'>");
        sb.Append(CreateTwitter(url, title));
        sb.Append("</li>");

        // Facebook +
        sb.Append("<li class='social-facebook' style='display:inline;'>");
        sb.Append(CreateFaceBook(url, title));
        sb.Append("</li>");

        sb.Append("</ul>");

        return sb.ToString();
    }

    public static string ToStr(object poStr)
    {
        if (poStr == null) return "";
        return poStr.ToString().Trim();
    }

    public static String ErorrLogin(String messa)
    {
        StringBuilder sb = new StringBuilder();

        sb.Append("<div class='Erorr'>");
        sb.Append("<table>");
        sb.Append("<tr>");
        sb.Append("<td>");
        sb.Append("<img alt='' src='../../images/EHTgiaodien/notice-alert.png' />");
        sb.Append("</td>");
        sb.Append("<td>");
        sb.Append(messa);
        sb.Append("</td>");
        sb.Append("</tr>");
        sb.Append("</table>");
        sb.Append("</div>");
        return sb.ToString();
    }

    public static String ApplySuccess(String messa)
    {
        StringBuilder sb = new StringBuilder();

        sb.Append("<div class='Success'>");
        sb.Append("<table>");
        sb.Append("<tr>");
        sb.Append("<td>");
        sb.Append("<img alt='' src='../../images/EHTgiaodien/notice-info.png' />");
        sb.Append("</td>");
        sb.Append("<td>");
        sb.Append(messa);
        sb.Append("</td>");
        sb.Append("</tr>");
        sb.Append("</table>");
        sb.Append("</div>");
        return sb.ToString();
    }

    public static String ChangeUrl(String input)
    {
        String url = "";
        String str = input.Split('.')[0].Trim(); ;
        Boolean pageNews = input.Contains("Page=2");
        Boolean listPro = input.Contains("list");
        Boolean ID = input.Contains("ID");
        if (str == "Trangchu")
            url = "Trang-chu.aspx";
        else if (str == "GioiThieu")
            url = "Gioi-thieu.aspx";
        else if (str == "ContactUs")
            url = "lien-he.aspx";
        else if (str == "Dichvu")
            url = "dich-vu.aspx";
        else if (str == "DoiTac")
            url = "doi-tac.aspx";
        else if (str == "TuyenDung")
            url = "tin-tuc.aspx";
        else if (str == "News")
            url = "tuyen-dung.aspx";        
        else
            url = input;
        return url;
    }
  
}

